package day06;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

//学生实体类javabean
public class student {
	 
    private String id;	//编号
  
    private String name;//姓名
  
    private String score;//分数

    private String grade;//等级
    //构造方法
    public Student() {
    }
    public Student(String id, String name, String score,String grade)
    {
        this.id = id;
        this.name = name;
        this.score = score;
        this.grade=grade;
    }
   //自动生成get/set方法
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getScore() {
        return score;
    }
    public void setScore(String score) {
        this.score = score;
    }
    public String getGrade() {
	return grade;
    }
    public void setGrade(String grade) {
	this.grade = grade;
    }

    //主函数main入口
    public static void main(String[] args) {
 	//new一个list列表
	ArrayList<Student> list = new ArrayList<Student>();
		
	//根据不同编号实现对应功能   
	while (true) {		 
	System.out.println("--------欢迎来到学生管理系统--------");
	System.out.println("1 查看所有学生");
	System.out.println("2 添加学生");
	System.out.println("3 删除学生");
	System.out.println("4 修改学生");
	System.out.println("5 按成绩降序查看所有学生");
	System.out.println("6 退出学生管理系统");
	System.out.println("请输入你的选择：");
		    
	@SuppressWarnings("resource")
	Scanner sc = new Scanner(System.in);
		        
	String choiceString = sc.nextLine();
		      
		switch (choiceString) {
		              case "1":
		                
		                  findAllStudent(list);
		                  break;
		              case "2":
		        
		                  addStudent(list);
		                  break;
		              case "3":
		          
		                  deleteStudent(list);
		                  break;
		              case "4":
		         
		                  updateStudent(list);
		                  break;
		              case "5":
		           
		                  orderByScore(list);
		                  break;
		              case "6":
		       
		                   System.out.println("谢谢你的使用");
		                   break;

		              default:
		                  System.out.println("请按6退出");
//		                  System.exit(0); 	// JVM退出
		                  break;
		          }
		      }
		  }
		 
                                  //修改学生信息
		  public static void updateStudent(ArrayList<Student> list) {
		      //实现一个输入流
		      Scanner sc = new Scanner(System.in);
		      System.out.println("请输入你要修改的学生的学号：");
		      String id = sc.nextLine();
		
		      int index = -1;
		
		      for (int x = 0; x < list.size(); x++) {
		
		          Student s = list.get(x);
		  
		          if (s.getId().equals(id)) {
		              index = x;
		              break;
		          }
		      }
		      if (index == -1) {
		          System.out.println("你要修改的学号对应的学生信息不存在,请回去重新你的选择");
		      }
		      else {
		          System.out.println("请输入学生新姓名：");
		          String name = sc.nextLine();
		          System.out.println("请输入学生新成绩：");
		          String score = sc.nextLine();
		          System.out.println("请输入学生班级：");
		          String grade = sc.nextLine();
		          // 创建学生对象
		          Student s = new Student();
		          s.setId(id);
		          s.setName(name);
		          s.setScore(score);
		          s.setGrade (grade);
		
		          list.set(index, s);
	
		          System.out.println("修改学生成功");
		      }
		  }
		 
		//删除学生信息
		  public static void deleteStudent(ArrayList<Student> list) {
		
		      Scanner sc = new Scanner(System.in);
		      System.out.println("请输入你要删除的学生的学号：");
		      String id = sc.nextLine();
		      
		      int index = -1;
	
		      for (int x = 0; x < list.size(); x++) {
		       
		          Student s = list.get(x);
		         
		          if (s.getId().equals(id)) {
		              index = x;
		              break;
		          }
		      }
		      if (index == -1) {
		          System.out.println("你要删除的学号对应的学生信息不存在");
		      } else {
		          list.remove(index);
		          System.out.println("删除学生成功");
		      }
		  }
		
		//增加学生信息
		  public static void addStudent(ArrayList<Student> list) {
	
		      Scanner sc = new Scanner(System.in);
		
		      String id;
		
		      while (true) {
		          System.out.println("请输入学生学号：");
		
		          id = sc.nextLine();
		       
		          boolean flag = false;
		     
		          for (int x = 0; x < list.size(); x++) {
		              Student s = list.get(x);
		           
		              if (s.getId().equals(id)) {
		                  flag = true; // 说明学号被占用了
		                  break;
		              }
		          }
		          if (flag) {
		              System.out.println("你输入的学号已经被占用,请重新输入");
		          }
		          else {
		              break; // 结束循环
		          }
		      }
		      System.out.println("请输入学生姓名：");
		      String name = sc.nextLine();
		      System.out.println("请输入学生成绩：");
		      String score = sc.nextLine();
		      System.out.println("请输入学生班级：");
		      String grade = sc.nextLine();
		
		      Student s = new Student();
		      s.setId(id);
		      s.setName(name);
		      s.setScore(score);
		      s.setGrade (grade);
		   
		      list.add(s);
		    
		      System.out.println("添加学生成功");
		  }
	  	 
		 //查找学生信息
		  public static void findAllStudent(ArrayList<Student> list)
		  {
		      
		      if (list.size() == 0) {
		          System.out.println("不好意思,目前没有学生信息可供查询,请回去重新选择你的操作");
		          return;
		      }
	
		      System.out.println("学号\t\t姓名\t成绩\t班级");
		      for (int x = 0; x < list.size(); x++)
		      {
		          Student s = list.get(x);
		          System.out.println(s.getId() + "\t"+ "\t"+s.getName() + "\t" +"\t"+ s.getScore()+"\t"+ s.getGrade() );
		      }
		  }

		  //成绩降序查询
		  private static void orderByScore(ArrayList<Student> list ) {
		      if (list.size() <= 0) {
		          System.out.println("数据库为空!!");
		          return;
		      }

	
		      ArrayList<Student> cList = (ArrayList<Student>) list.clone();

		      Collections.sort(cList, new Comparator<Student>() {

		          public int compare(Student s1, Student s2) {
		              Double id1 = new Double(s1.getScore());
		              Double id2 = new Double(s2.getScore());
		              Integer result = id1 < id2 ? 1 : id1 > id2 ? -1 : 0;
		              return result;
		          }
		      });

		
		      System.out.println("学号\t\t姓名\t成绩\t班级");
		      //依次循环打印学生信息
		      for (int i = 0; i < cList.size(); i++) {
		          Student s = cList.get(i);
		          System.out.println(s.getId() + "\t" + "\t" + s.getName() + "\t" + "\t" + s.getScore()+"\t"+ s.getGrade());
		      }
}}

